package itheima.zuoye.test1;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // 创建集合，存入手机对象
        List<Phone> list = new ArrayList<>();
        list.add(new Phone("华为Mate50Pro", 6739.00));
        list.add(new Phone("vivo X Fold2", 8999.00));
        list.add(new Phone("iphone14 128G", 5999.00));
        list.add(new Phone("vivo iQOO11", 4399.00));
        list.add(new Phone("荣耀90 5G", 2799.00));
        list.add(new Phone("红米K60E", 1999.00));
        list.add(new Phone("华为nove 11", 2799.00));
        list.add(new Phone("小米13 5G", 4299.00));


        while (true) {
            System.out.println("请输入您想购买的名称：");
            String brand = sc.next();
            Phone phone = null;
            // 遍历集合，找到那个手机
            // 【此处补充代码】！！！
            for (Phone p : list) {
                if (p.getBrand().equals(brand)) {
                    phone = p;
                    break;
                } else {
                    continue;
                }
            }
            // 判断是否有这个手机
            if (phone == null) {
                System.out.println("输入的手机未找到。");
                continue;
            }
            // 定义钢化膜价格
            // 【此处补充代码】！！！
            double screenProtectorPrice = 9.9;
            if (phone.getPrice() >= 3000) {
                screenProtectorPrice = 9.9;

            }
            //用BigDecimal类处理金额，避免精度丢失
            System.out.println("请输入优惠方案：\n\t1-满3000打98折 \n\t2-以旧换新立减500");
            int choose = sc.nextInt();
            switch (choose) {
                case 1:
                    if (phone.getPrice() >= 3000) {
                        BigDecimal price = BigDecimal.valueOf (phone.getPrice());
                        BigDecimal discount = price.multiply(new BigDecimal(0.02));
                        System.out.println("您选择的是满3000打98折，优惠价格为：" + discount.doubleValue());
                    } else {
                        System.out.println(phone.getBrand() + "手机不满3000,自动选择以旧换新：");
                        System.out.println("您选择的是以旧换新，新手机价格为：" + (BigDecimal.valueOf(phone.getPrice()-500)));
                    }
                    break;
                case 2:
                    System.out.println("您选择的是以旧换新，优惠价格为：500");
                    System.out.println("手机价格：" + (BigDecimal.valueOf(phone.getPrice() - 500)));
                    return;





            }
            // 计算手机和钢化膜总价
            // 【此处补充代码】！！！
            double totalPrice = 0;
            if (choose == 1) {
                BigDecimal phonePrice = BigDecimal.valueOf(phone.getPrice());
                totalPrice = phonePrice.multiply(new BigDecimal(0.98)).doubleValue() ;
                System.out.println("您购买的手机是：" + phone.getBrand() + "，总价为：" + totalPrice);
                System.out.println("请选择是否继续购物：\n\t1-继续购物 \n\t2-结账");
                int choose2 = sc.nextInt();
                if (choose2 == 1) {
                    continue;
                } else {
                    System.out.println("结账成功！");
                    break;
                }
            }

        }
    }
}
